package es.pawer.android.listacompras.data;

import java.util.List;

import es.pawer.android.listacompras.R;

import android.content.ContentValues;
import android.database.Cursor;

public class ItemDao extends AbstractLiteDAO<ItemTO> {
	
	private enum ColumnNames {
		ID,
		NAME
	}

	private static final String TABLENAME = "ITEM";
	
	private enum LinkColumnNames { 
		LIST_ID,
		ITEM_ID, 
		ORDER_POINTS,
		PRICE,
		UNITS 
	};
	
	private static final String LINK_TABLE="LIST_ITEM";

	/**
	 * Columns of the Item table: NAME, PRICE, CURRENCY, ORDER_POINTS and
	 * LIST_ID (used to find goods)
	 */
	@Override
	protected ItemTO obtainElement(Cursor c, int pos) {
		ItemTO g = null;
		if (c != null && c.moveToPosition(pos)) {
			g = new ItemTO();
			g.setId(c.getInt(c.getColumnIndex(ColumnNames.ID.name())));
			g.setName(c.getString(c.getColumnIndex(ColumnNames.NAME.name())));

//			item linked with a list
			if (c.getColumnCount()>2) {
				g.setOrderPoints(c.getInt(c.getColumnIndex(LinkColumnNames.ORDER_POINTS.name())));
				int i=c.getColumnIndex(LinkColumnNames.PRICE.name());
				if (!c.isNull(i)) {
					g.setPrice(c.getInt(c.getColumnIndex(LinkColumnNames.PRICE.name())));
				} else {
					g.setPrice(0);
				}
				g.setUnits(c.getInt(c.getColumnIndex(LinkColumnNames.UNITS.name())));
				g.setListId(c.getLong(c.getColumnIndex(LinkColumnNames.LIST_ID.name())));
			}
		}
		return g;
	}

	@Override
	public int delete(ItemTO item) throws DataException {
		if (item==null) {
			throw new DataException("ItemTO is null or is empty");
		}
		int i =0;
		
		i = MyDatabaseHelper.getWritableDB().delete(getTableName(), "ID=?",
					new String[] { String.valueOf(item.getId()) });
		return i;
	}

	@Override
	public String getTableName() {
		return TABLENAME;
	}
	
	
	@Override
	protected ContentValues getValues(ItemTO item) throws DataException {
		if (item==null || item.getName()==null) {
			throw new DataException("ItemTO is null or is empty");
		}
		ContentValues cv = new ContentValues();
		cv.put(ColumnNames.ID.name(), item.getId());
		cv.put(ColumnNames.NAME.name(), item.getName());
		return cv;
	}
	
	/**
	 * Similar to ItemDao.getValues, but extracting values for the LIST_ITEM table.
	 * @param item
	 * @return
	 * @throws DataException
	 */
	protected ContentValues getLinkValues(ItemTO item) throws DataException {
		if (item==null || item.getName()==null) {
			throw new DataException("ItemTO is null or is empty");
		}
		ContentValues cv = new ContentValues();
		cv.put(LinkColumnNames.ITEM_ID.name(), item.getId());
		cv.put(LinkColumnNames.LIST_ID.name(), item.getListId());
		cv.put(LinkColumnNames.ORDER_POINTS.name(), item.getOrderPoints());
		cv.put(LinkColumnNames.PRICE.name(), item.getPrice());
		cv.put(LinkColumnNames.UNITS.name(), item.getUnits());
		return cv;
	}

	/**
	 * Method to update a linked item
	 * @param item
	 * @return
	 * @throws DataException
	 */
	public int updateLink(ItemTO item) throws DataException {
		ContentValues values = getLinkValues(item);
		int rows=MyDatabaseHelper.getWritableDB().update(
				LINK_TABLE, values, LinkColumnNames.ITEM_ID+"=? AND "+LinkColumnNames.LIST_ID+"=? ",
				new String[] { String.valueOf(item.getId()),String.valueOf(item.getListId()) });
		return rows;
	}

	@Override
	public ItemTO getById(ItemTO item) throws DataException {
		if (item==null || (item.getName()==null)) {
			throw new DataException("ItemTO is null or is empty");
		}
		Cursor c = MyDatabaseHelper.getWritableDB().query(getTableName(), null,
				ColumnNames.ID.name() + "=?", new String[] { String.valueOf(item.getId()) },
				null, null, null);
		item = this.obtainElement(c, 0);
		return item;
	}

	/**
	 * 
	 * @param listId The list
	 * @return a sorted list of items. The order used is the order points previously stored.
	 */
	public List<ItemTO> getByList(long listId) {
		String sql=MyDatabaseHelper.getInstance().getStringResource(R.string.database_sql_select_item);
		Cursor c=MyDatabaseHelper.getWritableDB().rawQuery(sql,	
				new String[] {String.valueOf(listId),LinkColumnNames.ORDER_POINTS.name()});
		return this.createList(c);
	}

	@Override
	public int update(ItemTO element) throws DataException {
		// TODO Auto-generated method stub
		return 0;
	}
	
	
	@Override
	public List<ItemTO> getAll() {
		Cursor myCursor=MyDatabaseHelper.getWritableDB().query(
				getTableName(), null, null, null, null, null, ColumnNames.NAME.name());
		return this.createList(myCursor);
		
	}

}
